﻿@model WaiGaoQiaoWeb.Models.YaxMachineVM
@{
    ViewBag.Title = "YaxMachineIndex";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section Head
{
    <script type="text/javascript" language="javascript">
        var maiheight = document.documentElement.clientHeight;
        var mainWidth = document.documentElement.clientWidth - 10; // 减去边框和左边的宽度
        $(document).ready(function () {
            var otherpm = $("#title").height() + $("#findDiv").height(); // 290;
            var gh = maiheight - otherpm - 80;
            var option = {
                height: gh,
                width: mainWidth,
                url: '@Url.Action("GetYaxMachineList")', // 参数要放在）括号的外面
                dataType: 'json',
                colModel: [
                    { display: 'MacID', name: 'MacID', width: 70, sortable: false, hide: true, align: 'left', iskey: true },
                    { display: '设备组名称', name: 'NodeName', width: 100, sortable: false, hide: false, align: 'left' },
					{ display: '设备编号', name: 'MacNo', width: 80, sortable: false, hide: false, align: 'left' },
                    { display: '设备类型', name: 'MacTypeName', width: 150, sortable: false, hide: false, align: 'left' },
                    { display: '设备描述', name: 'MacDescribe', width: 150, sortable: false, hide: false, align: 'left' },
                    { display: '通讯方式', name: 'CommStyle_', width: 50, sortable: false, hide: false, align: 'left' },
                    { display: '端口号', name: 'PortNo', width: 50, sortable: false, hide: false, align: 'left' },
                    { display: '波特率', name: 'BaudRate', width: 50, sortable: false, hide: false, align: 'left' },
                    { display: 'IP地址', name: 'IPAddress', width: 130, sortable: false, hide: false, align: 'left' },
                    { display: '网关', name: 'GetwayIP', width: 130, sortable: false, hide: false, align: 'left' },
                    { display: '子网掩码', name: 'SubIPAddr', width: 130, sortable: false, hide: false, align: 'left' }
				],
                buttons: [
							{ name: 'Add', displayname: "新增设备", title: "新增设备", bclass: 'add', onpress: toolbarItemAdd_onclick },
                            { name: 'Delete', displayname: "删除设备", title: "删除设备", bclass: 'edit', onpress: toolbarItemDel_onclick },
                            { name: 'Edit', displayname: "修改设备", title: "修改设备", bclass: 'edit', onpress: toolbarItemEdit_onclick },
							{ separator: true }
						 ],
                title: false,
                singleselected: false,
                usepager: true,
                resizable: true,
                page: 1, //默认当前页  
                total: 1, //总页面数  
                useRp: true, //是否可以动态设置每页显示的结果数  
                rp: 20, // 每页默认的结果数  
                rpOptions: [10, 15, 20], //可选择设定的每页结果数
                showcheckbox: true,
                selectedonclick: true,
                rowbinddata: true,
                query: '',
                hideOnSubmit: true, //隐藏提交  
                autoload: true
            };
            $("#YaxMachineList").flexigrid(option);
            $("#butfind").click(function (e) {
                var vNodeName = $("#txtNodeName").val();
                var vMacTypeName = $("#MacTypeName :selected").val();
                option.query = vNodeName + ',' + vMacTypeName;
                option.newp = 0;
                $('#YaxMachineList').flexOptions(option).flexReload();
            });
            $("#txtNodeName").bind('keydown', function (e) {
                var key = e.which;
                if (key == 13) {
                    e.preventDefault();
                    var vNodeName = $("#txtNodeName").val();
                    var vMacTypeName = $("#MacTypeName").val();
                    option.query = vNodeName + ',' + vMacTypeName;
                    option.newp = 0;
                    $('#YaxMachineList').flexOptions(option).flexReload();
                }
            });
            $("#MacTypeName").bind('keydown', function (e) {
                var key = e.which;
                if (key == 13) {
                    e.preventDefault();
                    var vNodeName = $("#txtNodeName").val();
                    var vMacTypeName = $("#MacTypeName").val();
                    option.query = vNodeName + ',' + vMacTypeName;
                    option.newp = 0;
                    $('#YaxMachineList').flexOptions(option).flexReload();
                }
            });
            function toolbarItemAdd_onclick(cmd, grid) {
                OpenModelWindow('@Url.Action("AddYaxMachine")',
                                   { width: 450, height: 500, caption: "新增设备", onclose: refresh });
            }  // end of toolbarItemAdd_onclick

            function toolbarItemEdit_onclick(cmd, grid) {
                var items = $("#YaxMachineList").getSelectedRows();
                if (items.length == 0) {
                    showLoadingMsg("请选择要编辑的设备!", { left: 400, top: 6 }, true, 3000);
                    return false;
                }
                if (items.length > 1) {
                    showLoadingMsg("只能选择一个要编辑的设备!", { left: 400, top: 6 }, true, 3000);
                    return false;
                }
                var ID = items[0][0];

                $.ajax({
                    type: "POST",
                    url: '@Url.Action("SelYaxMachineEdit")',
                    data: { ID: ID },
                    dataType: "json",
                    async: false,
                    success: function (data) {
                        if (data >0) {
                            showLoadingMsg("系统保留项不能编辑!", { left: 400, top: 6 }, true, 3000);
                        }
                        else
                        {
                            OpenModelWindow('@Url.Action("EditYaxMachine")?id=' + ID,
                            { width: 450, height: 500, caption: "修改设备", onclose: refresh });
                        }
                    },
                });
            }
            function toolbarItemDel_onclick(cmd, grid) {
                var items = $("#YaxMachineList").getSelectedRows();
                if (items.length == 0) {
                    showLoadingMsg("请选择要删除的设备!", { left: 400, top: 6 }, true, 3000);
                    return false;
                }
                if (!confirm("确定要删除设备吗？")) {
                    return false;
                }
                var rowcount = 0;
                for (var i = 0; i < items.length; i++) {
                    var ID = items[i][0];
                    $.ajax({
                        type: "POST",
                        url: '@Url.Action("DeleteYaxMachine")',
                        data: { ID: ID },
                        dataType: "json",
                        async: false,
                        success: function (data) {
                            if (data == "-2") {
                                showLoadingMsg("系统保留项不能删除!", { left: 400, top: 6 }, true, 3000);
                                return;
                            }
                            if (data) {
                                rowcount = rowcount + data;
                            }
                        },
                        error: function () { }
                    });   // end of ajax                    
                }
                refreshGrid();
                showLoadingMsg("共有" + rowcount.toString() + "个设备被删除!", { left: 400, top: 6 }, true, 3000);
            }  // end of toolbarItemUp_onclick
            function refreshGrid() {
                $("#YaxMachineList").flexReload();
            }
            function refresh() {
                showLoadingMsg("保存成功", { left: 200, top: 6 }, true, 3000);
                $("#YaxMachineList").flexReload();
            }
        });    //end document ready() 
    </script>
}
<div id="container_sx">
    <div id="mainContent">
        <div id="findDiv">
            <table>
                <tr>
                    <td style="text-align: right;">
                        &nbsp;设备组名称：
                    </td>
                    <td style="text-align: left;">
                        @Html.TextBox("txtNodeName", "", new { @style = "width:180px;" })
                    </td>
                    <td style="text-align: right;">
                        &nbsp;设备类型名称：
                    </td>
                    <td style="text-align: left;">
                        @*@Html.TextBox("txtMacTypeName", "", new { @style = "width:180px;" })*@
                        @Html.DropDownList("MacTypeName",
                                                      ViewData["MacTypeName"] as IEnumerable<SelectListItem>,
                                        new { @style = "width:158px; height:22px; font-size:13px;" })
                    </td>
                    <td colspan="2">
                        <a id="butfind" class="button" href="javascript:void(0);"><span>查询</span></a>
                    </td>
                </tr>
            </table>
        </div>
        <table id="YaxMachineList" style="display: none;">
        </table>
    </div>
</div>
